<?php

use think\migration\Migrator;

class ChatCategory extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected $name = 'chat_category';

    public function up(): void
    {
        $table = $this->table($this->name, [
            'comment'     => '对话分类',
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id']
        ]);
        $table->addColumn('type', 'string', ['limit' => 30, 'null' => false, 'comment' => '分类类型'])
            ->addColumn('pid', 'integer', ['limit' => 11, 'signed'  => false, 'default' => 0, 'null' => false, 'comment' => '上级ID'])
            ->addColumn('name', 'string', ['limit' => 50, 'default' => '', 'null' => false, 'comment' => '名称'])
            ->addColumn('icon', 'string', ['limit' => 255, 'default' => '', 'null' => false, 'comment' => '图标'])
            ->addColumn('listorder', 'integer', ['limit' => 11, 'signed'  => false, 'default' => 0, 'null' => false, 'comment' => '排序'])
            ->addColumn('status', 'tinyinteger', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '状态'])
            ->addColumn('remark', 'string', ['limit' => 200, 'null' => true, 'comment' => '备注'])
            ->addColumn('create_at', 'datetime', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_at', 'datetime', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['type', 'status', 'listorder'], ['name' => 'type'])
            ->create();
    }

    public function down(): void
    {
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
